package controll

import (
	"github.com/gin-gonic/gin"
	"github.com/gorilla/websocket"
	"im_project/configs"
	"net/http"
)

func Login(c *gin.Context) {
	//鉴权后面做 todo

	name := c.Query("name")
	if name == "" {
		c.JSON(502, gin.H{"错误": "没有必要的参数"})
		return
	}
	//升级为ws
	ws, err := (&websocket.Upgrader{}).Upgrade(c.Writer, c.Request, nil)
	if err != nil {
		panic(err)
	}

	client := &configs.Client{Name: name, Conn: ws}

	configs.Clients[*client] = true
	configs.ClientsSync.Store(name, client)

	// 将用户加入消息放入消息通道
	//var msg configs.Message
	//msg.Name = client.Name
	//msg.EventType = 1
	//msg.Message = fmt.Sprintf("%s join in, there are %d preson in room", client.Name, len(configs.Clients))
	//configs.MessagesChan <- msg

	go client.ReadMsg()

}
func Chat(c *gin.Context) {

	name := c.Query("name")
	c.Set("name", name)
	c.HTML(http.StatusOK, "chatRoom.html", name)
	//c.Chat(http.StatusPermanentRedirect,"/views/chatRoom.html?name=")

}
